<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class Menus extends Default_My_Model {
    
    var $table_name = "mt_menu";
    
    public function __construct()
    {
        parent::__construct();
    }
    
    public function getMenu($id_induk = "", $except = ""){
        if($except != "")
            $except = " AND id_menu <> $except";
        if($id_induk == "")
            $sql = "SELECT * FROM ".$this->table_name." WHERE id_menu_induk is null AND id_menu != 7 $except ORDER BY urutan";
        else 
            $sql = "SELECT * FROM ".$this->table_name." WHERE id_menu_induk = '$id_induk' $except ORDER BY urutan";
        
        return $this->db->query($sql);
    }
    
    public function getMenuDetail($kode){
        $result = $this->db->query("SELECT * FROM ".$this->table_name." WHERE id_menu = '$kode'");
        return $result->row();
    }
    
    public function getFirstLast($kode){
        $result = $this->db->query("select min(urutan) as first, max(urutan) as last from ".$this->table_name." where id_menu_induk = '$kode'");
        return $result->row();
    }
    
    public function updateMenu($id_menu, $arrVals) {
        $this->db->where('id_menu', $id_menu);
        $this->db->update($this->table_name, $arrVals);
    }
    
    public function addMenu($arrVals) {
        $this->db->insert($this->table_name, $arrVals);
    }
    
    public function shiftMenu($kode, $urutan = 0, $inc = 1){
        if($urutan > 0)
            $this->db->query("UPDATE ".$this->table_name." SET urutan = urutan + $inc where id_menu_induk = '$kode' and urutan >= $urutan");
    }
    
    public function updateUrutan($arr_menu){
        foreach($arr_menu as $key => $row)
            $this->db->query("update ".$this->table_name." set urutan = ".($key + 1)." WHERE id_menu = '$row'");
    }
    
}

?>
